package com.yongcheon.sms_manager.ui.sms_spam.list;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.data.sms_spam.SMSData;
import com.yongcheon.sms_manager.data.sms_spam.SpamData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamRegistDlg;
import com.yongcheon.sms_manager.utils.DefaultAppUtil;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-27.
 */
public class SMSLongClickListener  implements View.OnLongClickListener{
    SMSVO smsvo;
    ContactVO contactvo;
    View view;

    public SMSLongClickListener(SMSVO smsvo, ContactVO contactvo, View view){
        super();
        this.smsvo = smsvo;
        this.contactvo = contactvo;
        this.view = view;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.delete_this_sms), v.getContext().getResources().getString(R.string.delete_same_number),
//                v.getContext().getResources().getString(R.string.register_spam_one),
                              v.getContext().getResources().getString(R.string.register_spam_total)};

        String from = (contactvo != null) ? contactvo.getName() : smsvo.getAddress();
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(from)
                .setItems(MENU_LIST, new SMSDialogOnClickListener(v.getContext()))
                .create();

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    public class SMSDialogOnClickListener implements  DialogInterface.OnClickListener{
        private Context context;
        private SMSData smsData;
        private SpamData spamData;
        private SpamNumberData spamNumData;
        private SQLiteDatabase database;

        public SMSDialogOnClickListener(Context context){
            super();
            this.context = context;
            this.smsData = new SMSData();
            this.spamData = new SpamData(context);
            this.spamNumData = new SpamNumberData(context);
            this.database = SpamDBManager.getDatabase(context);
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0 :
                    if(DefaultAppUtil.isWritable(context)) smsData.delete(SMSLongClickListener.this.smsvo.get_Id(), context, null);
                break;
                case 1:
                    if(DefaultAppUtil.isWritable(context)) smsData.deleteByPhoneNum(SMSLongClickListener.this.smsvo.getAddress(), context,  null);
                break;
                case 2 :
                    if(DefaultAppUtil.isWritable(context))  {
                        SpamNumVO spamNumVO = getSpamNumInfo(SMSLongClickListener.this.smsvo.getAddress());
                        if(spamNumVO != null){
                            registAllSpam(spamNumVO);
                        }else{
                            spamNumVO = new SpamNumVO();
                            spamNumVO.setAddress(SMSLongClickListener.this.smsvo.getAddress());
                            spamNumVO.setCategory_1(context.getResources().getString(R.string.user_regist));
                            spamNumVO.setDesc(SMSLongClickListener.this.smsvo.getBody());
                            SpamRegistDlg.showSpamRegistDlg(context, view, spamNumVO, this, false);
                        }
                    }
                break;
                case 3 :
                    if(DefaultAppUtil.isWritable(context)) {
                        SpamNumVO spamNumVO = getSpamNumInfo(SMSLongClickListener.this.smsvo.getAddress());
                        if(spamNumVO != null){
                            registOnlyOneSpam(spamNumVO);
                        }else{
                            spamNumVO = new SpamNumVO();
                            spamNumVO.setAddress(SMSLongClickListener.this.smsvo.getAddress());
                            spamNumVO.setCategory_1(context.getResources().getString(R.string.user_regist));
                            spamNumVO.setDesc(SMSLongClickListener.this.smsvo.getBody());
                            spamNumVO.setEtc("1");
                            SpamRegistDlg.showSpamRegistDlg(context, view, spamNumVO, this, false);
                        }
                    }
                    break;
            }
        }

        public SpamNumVO getSpamNumInfo(String address){
            SpamNumVO spamNumVO = null;
            ArrayList<SpamNumVO> list = spamNumData.selectReturnVo(address, context, database);
            if(list.size() > 0) spamNumVO = list.get(0);
            return spamNumVO;
        }

        public void registOnlyOneSpam(SpamNumVO spamNumVO){
            ArrayList<SMSVO> smsList = smsData.selectById(String.valueOf(SMSLongClickListener.this.smsvo.get_Id()), context, null);
            registSpam(smsList, spamNumVO);
        }

        public void registAllSpam(SpamNumVO spamNumVO){
            ArrayList<SMSVO> smsList = smsData.select(SMSLongClickListener.this.smsvo.getAddress(), context,  null);
            registSpam(smsList, spamNumVO);
        }

        public void registSpam(ArrayList<SMSVO> smsList, SpamNumVO spamNumVO){
            try {
                database.beginTransaction();

                for (int i = 0; i < smsList.size(); i++) {
                    SMSVO smsvo = smsList.get(i);
                    smsvo.setSpam_category(spamNumVO.getCategory_2());
                    smsvo.setSpam_desc(spamNumVO.getSubject());
                    spamData.insert(smsList.get(i), context, database);
                }

                spamNumData.insertNotExist(spamNumVO, context, database);

                for (int i = 0; i < smsList.size(); i++) {
                    smsData.delete(smsList.get(i).get_Id(), context,  null);
                }

                database.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                database.endTransaction();
            }
        }

    }
}
